package com.codeazur.as3swf.tags
{
   import com.codeazur.as3swf.SWFData;
   import com.codeazur.as3swf.data.SWFMorphFillStyle;
   import com.codeazur.as3swf.data.SWFMorphLineStyle;
   import com.codeazur.as3swf.data.SWFRectangle;
   import com.codeazur.as3swf.data.SWFShape;
   import com.codeazur.as3swf.data.SWFShapeRecord;
   import com.codeazur.as3swf.data.SWFShapeRecordCurvedEdge;
   import com.codeazur.as3swf.data.SWFShapeRecordStraightEdge;
   import com.codeazur.as3swf.data.SWFShapeRecordStyleChange;
   import com.codeazur.as3swf.exporters.core.IShapeExporter;
   import com.codeazur.utils.StringUtils;
   
   public class TagDefineMorphShape implements IDefinitionTag
   {
      
      public static const TYPE:uint = 46;
       
      
      public var startBounds:SWFRectangle;
      
      public var endBounds:SWFRectangle;
      
      public var startEdges:SWFShape;
      
      public var endEdges:SWFShape;
      
      protected var _characterId:uint;
      
      protected var _morphFillStyles:Vector.<SWFMorphFillStyle>;
      
      protected var _morphLineStyles:Vector.<SWFMorphLineStyle>;
      
      public function TagDefineMorphShape()
      {
         super();
         this._morphFillStyles = new Vector.<SWFMorphFillStyle>();
         this._morphLineStyles = new Vector.<SWFMorphLineStyle>();
      }
      
      public function get characterId() : uint
      {
         return this._characterId;
      }
      
      public function set characterId(param1:uint) : void
      {
         this._characterId = param1;
      }
      
      public function get morphFillStyles() : Vector.<SWFMorphFillStyle>
      {
         return this._morphFillStyles;
      }
      
      public function get morphLineStyles() : Vector.<SWFMorphLineStyle>
      {
         return this._morphLineStyles;
      }
      
      public function parse(param1:SWFData, param2:uint, param3:uint, param4:Boolean = false) : void
      {
         var _loc6_:uint = 0;
         this._characterId = param1.readUI16();
         this.startBounds = param1.readRECT();
         this.endBounds = param1.readRECT();
         var _loc5_:uint = param1.readUI32();
         var _loc7_:uint = param1.readUI8();
         if(_loc7_ == 255)
         {
            _loc7_ = param1.readUI16();
         }
         _loc6_ = 0;
         while(_loc6_ < _loc7_)
         {
            this._morphFillStyles.push(param1.readMORPHFILLSTYLE());
            _loc6_++;
         }
         var _loc8_:uint = param1.readUI8();
         if(_loc8_ == 255)
         {
            _loc8_ = param1.readUI16();
         }
         _loc6_ = 0;
         while(_loc6_ < _loc8_)
         {
            this._morphLineStyles.push(param1.readMORPHLINESTYLE());
            _loc6_++;
         }
         this.startEdges = param1.readSHAPE();
         this.endEdges = param1.readSHAPE();
      }
      
      public function publish(param1:SWFData, param2:uint) : void
      {
         var _loc5_:uint = 0;
         var _loc3_:SWFData = new SWFData();
         _loc3_.writeUI16(this.characterId);
         _loc3_.writeRECT(this.startBounds);
         _loc3_.writeRECT(this.endBounds);
         var _loc4_:SWFData = new SWFData();
         var _loc6_:uint = this._morphFillStyles.length;
         if(_loc6_ > 254)
         {
            _loc4_.writeUI8(255);
            _loc4_.writeUI16(_loc6_);
         }
         else
         {
            _loc4_.writeUI8(_loc6_);
         }
         _loc5_ = 0;
         while(_loc5_ < _loc6_)
         {
            _loc4_.writeMORPHFILLSTYLE(this._morphFillStyles[_loc5_]);
            _loc5_++;
         }
         var _loc7_:uint = this._morphLineStyles.length;
         if(_loc7_ > 254)
         {
            _loc4_.writeUI8(255);
            _loc4_.writeUI16(_loc7_);
         }
         else
         {
            _loc4_.writeUI8(_loc7_);
         }
         _loc5_ = 0;
         while(_loc5_ < _loc7_)
         {
            _loc4_.writeMORPHLINESTYLE(this._morphLineStyles[_loc5_]);
            _loc5_++;
         }
         _loc4_.writeSHAPE(this.startEdges);
         _loc3_.writeUI32(_loc4_.length);
         _loc3_.writeBytes(_loc4_);
         _loc3_.writeSHAPE(this.endEdges);
         param1.writeTagHeader(this.type,_loc3_.length);
         param1.writeBytes(_loc3_);
      }
      
      public function clone() : IDefinitionTag
      {
         var _loc1_:TagDefineMorphShape = new TagDefineMorphShape();
         throw new Error("Not implemented yet.");
      }
      
      public function export(param1:IShapeExporter = null, param2:Number = 0) : void
      {
         var _loc3_:uint = 0;
         var _loc7_:SWFShapeRecord = null;
         var _loc8_:SWFShapeRecord = null;
         var _loc9_:SWFShapeRecord = null;
         var _loc10_:SWFShapeRecordStyleChange = null;
         var _loc11_:SWFShapeRecordStyleChange = null;
         var _loc12_:SWFShapeRecordStraightEdge = null;
         var _loc13_:SWFShapeRecordStraightEdge = null;
         var _loc14_:SWFShapeRecordCurvedEdge = null;
         var _loc15_:SWFShapeRecordCurvedEdge = null;
         var _loc4_:uint = 0;
         var _loc5_:SWFShape = new SWFShape();
         var _loc6_:uint = this.startEdges.records.length;
         _loc3_ = 0;
         while(_loc3_ < _loc6_)
         {
            _loc7_ = this.startEdges.records[_loc3_];
            if(_loc7_.type == SWFShapeRecord.TYPE_STYLECHANGE && !SWFShapeRecordStyleChange(_loc7_).stateMoveTo)
            {
               _loc5_.records.push(_loc7_.clone());
            }
            else
            {
               _loc8_ = this.endEdges.records[_loc4_++];
               if(_loc7_.type == SWFShapeRecord.TYPE_CURVEDEDGE && _loc8_.type == SWFShapeRecord.TYPE_STRAIGHTEDGE)
               {
                  _loc8_ = this.convertToCurvedEdge(_loc8_ as SWFShapeRecordStraightEdge);
               }
               else if(_loc7_.type == SWFShapeRecord.TYPE_STRAIGHTEDGE && _loc8_.type == SWFShapeRecord.TYPE_CURVEDEDGE)
               {
                  _loc7_ = this.convertToCurvedEdge(_loc7_ as SWFShapeRecordStraightEdge);
               }
               switch(_loc7_.type)
               {
                  case SWFShapeRecord.TYPE_STYLECHANGE:
                     _loc10_ = _loc7_.clone() as SWFShapeRecordStyleChange;
                     _loc11_ = _loc8_ as SWFShapeRecordStyleChange;
                     _loc10_.moveDeltaX = _loc10_.moveDeltaX + (_loc11_.moveDeltaX - _loc10_.moveDeltaX) * param2;
                     _loc10_.moveDeltaY = _loc10_.moveDeltaY + (_loc11_.moveDeltaY - _loc10_.moveDeltaY) * param2;
                     _loc9_ = _loc10_;
                     break;
                  case SWFShapeRecord.TYPE_STRAIGHTEDGE:
                     _loc12_ = _loc7_.clone() as SWFShapeRecordStraightEdge;
                     _loc13_ = _loc8_ as SWFShapeRecordStraightEdge;
                     _loc12_.deltaX = _loc12_.deltaX + (_loc13_.deltaX - _loc12_.deltaX) * param2;
                     _loc12_.deltaY = _loc12_.deltaY + (_loc13_.deltaY - _loc12_.deltaY) * param2;
                     if(_loc12_.deltaX != 0 && _loc12_.deltaY != 0)
                     {
                        _loc12_.generalLineFlag = true;
                        _loc12_.vertLineFlag = false;
                     }
                     else
                     {
                        _loc12_.generalLineFlag = false;
                        _loc12_.vertLineFlag = _loc12_.deltaX == 0;
                     }
                     _loc9_ = _loc12_;
                     break;
                  case SWFShapeRecord.TYPE_CURVEDEDGE:
                     _loc14_ = _loc7_.clone() as SWFShapeRecordCurvedEdge;
                     _loc15_ = _loc8_ as SWFShapeRecordCurvedEdge;
                     _loc14_.controlDeltaX = _loc14_.controlDeltaX + (_loc15_.controlDeltaX - _loc14_.controlDeltaX) * param2;
                     _loc14_.controlDeltaY = _loc14_.controlDeltaY + (_loc15_.controlDeltaY - _loc14_.controlDeltaY) * param2;
                     _loc14_.anchorDeltaX = _loc14_.anchorDeltaX + (_loc15_.anchorDeltaX - _loc14_.anchorDeltaX) * param2;
                     _loc14_.anchorDeltaY = _loc14_.anchorDeltaY + (_loc15_.anchorDeltaY - _loc14_.anchorDeltaY) * param2;
                     _loc9_ = _loc14_;
                     break;
                  case SWFShapeRecord.TYPE_END:
                     _loc9_ = _loc7_.clone();
               }
               _loc5_.records.push(_loc9_);
            }
            _loc3_++;
         }
         _loc3_ = 0;
         while(_loc3_ < this.morphFillStyles.length)
         {
            _loc5_.fillStyles.push(this.morphFillStyles[_loc3_].getMorphedFillStyle(param2));
            _loc3_++;
         }
         _loc3_ = 0;
         while(_loc3_ < this.morphLineStyles.length)
         {
            _loc5_.lineStyles.push(this.morphLineStyles[_loc3_].getMorphedLineStyle(param2));
            _loc3_++;
         }
         _loc5_.export(param1);
      }
      
      protected function convertToCurvedEdge(param1:SWFShapeRecordStraightEdge) : SWFShapeRecordCurvedEdge
      {
         var _loc2_:SWFShapeRecordCurvedEdge = new SWFShapeRecordCurvedEdge();
         _loc2_.controlDeltaX = param1.deltaX / 2;
         _loc2_.controlDeltaY = param1.deltaY / 2;
         _loc2_.anchorDeltaX = param1.deltaX;
         _loc2_.anchorDeltaY = param1.deltaY;
         return _loc2_;
      }
      
      public function get type() : uint
      {
         return TYPE;
      }
      
      public function get name() : String
      {
         return "DefineMorphShape";
      }
      
      public function get version() : uint
      {
         return 3;
      }
      
      public function get level() : uint
      {
         return 1;
      }
      
      public function toString(param1:uint = 0, param2:uint = 0) : String
      {
         var _loc3_:uint = 0;
         var _loc4_:String = StringUtils.repeat(param1 + 2);
         var _loc5_:String = StringUtils.repeat(param1 + 4);
         var _loc6_:String = Tag.toStringCommon(this.type,this.name,param1) + "ID: " + this.characterId;
         _loc6_ = _loc6_ + ("\n" + _loc4_ + "Bounds:");
         _loc6_ = _loc6_ + ("\n" + _loc5_ + "StartBounds: " + this.startBounds.toString());
         _loc6_ = _loc6_ + ("\n" + _loc5_ + "EndBounds: " + this.endBounds.toString());
         if(this._morphFillStyles.length > 0)
         {
            _loc6_ = _loc6_ + ("\n" + _loc4_ + "FillStyles:");
            _loc3_ = 0;
            while(_loc3_ < this._morphFillStyles.length)
            {
               _loc6_ = _loc6_ + ("\n" + _loc5_ + "[" + (_loc3_ + 1) + "] " + this._morphFillStyles[_loc3_].toString());
               _loc3_++;
            }
         }
         if(this._morphLineStyles.length > 0)
         {
            _loc6_ = _loc6_ + ("\n" + _loc4_ + "LineStyles:");
            _loc3_ = 0;
            while(_loc3_ < this._morphLineStyles.length)
            {
               _loc6_ = _loc6_ + ("\n" + _loc5_ + "[" + (_loc3_ + 1) + "] " + this._morphLineStyles[_loc3_].toString());
               _loc3_++;
            }
         }
         _loc6_ = _loc6_ + this.startEdges.toString(param1 + 2);
         _loc6_ = _loc6_ + this.endEdges.toString(param1 + 2);
         return _loc6_;
      }
   }
}
